package com.wutuo.school.provider.dao;

import com.wutuo.school.api.request.ListSchoolRequest;
import com.wutuo.school.api.request.SchoolRequest;
import org.apache.ibatis.jdbc.SqlBuilder;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * 校区 dao sql 层
 *
 * @author lvh
 * @version 1.0
 */

public class SchoolSqlProvider extends BaseSchoolSqlProvider {

    public String lisForPage(ListSchoolRequest request) {

        BEGIN();
        getSelected();
        FROM("school a ");
        getComonJoin(request);
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());

        if (request.getPage() != null && request.getSize() != null) {
            sql.append(" limit #{index,jdbcType=INTEGER} ,  #{size,jdbcType=INTEGER} ");
        }

        return sql.toString();
    }

    public String list(SchoolRequest request) {

        BEGIN();
        getSelected();
        FROM("school a");
        getComonJoin(request);
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        return sql.toString();
    }

    public void getSelected() {

        SELECT(
            "a.id,school_name as schoolName,phone as phone,logo as logo,address as address,province as province,city as city,county as county,create_by as createBy,create_date as createDate,update_by as updateBy,update_date as updateDate,remarks as baiduId,del_flag as delFlag");
    }

    public void getComonJoin(SchoolRequest request) {

        if (request.getIds() != null) {
            WHERE(" a.id in (" + request.getIds() + ")");
        }

        if (request.getRemarks() != null) {
            WHERE(" a.remarks = #{remarks,jdbcType=VARCHAR}");
        }

    }

}